package cn.iocoder.an.module.business.controller.admin.equipmentkeepsettingtype;

import cn.iocoder.an.module.business.controller.admin.equipmentkeepsetting.vo.EquipmentSettingBindImportExcelVO;
import cn.iocoder.an.module.business.controller.admin.equipmentkeepsetting.vo.EquipmentSettingImportExcelVO;
import cn.iocoder.an.module.business.controller.admin.equipmentkeeptype.vo.EquipmentKeepTypeImportExcelVO;
import cn.iocoder.an.module.business.controller.admin.equipmentmanage.vo.ImportRespVO;
import cn.iocoder.an.module.business.dal.dataobject.equipmentkeepsetting.EquipmentKeepSettingDO;
import cn.iocoder.an.module.business.dal.dataobject.equipmentkeeptype.EquipmentKeepTypeDO;
import cn.iocoder.an.module.business.dal.mysql.equipmentkeepsetting.EquipmentKeepSettingMapper;
import cn.iocoder.an.module.business.enums.MaintenanceStatusEnum;
import cn.iocoder.an.module.business.service.equipmentkeepsetting.EquipmentKeepSettingService;
import cn.iocoder.an.module.business.service.equipmentkeeptype.EquipmentKeepTypeService;
import io.swagger.v3.oas.annotations.Parameters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;
import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

import cn.iocoder.an.framework.excel.core.util.ExcelUtils;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.an.module.business.controller.admin.equipmentkeepsettingtype.vo.*;
import cn.iocoder.an.module.business.dal.dataobject.equipmentkeepsettingtype.EquipmentKeepSettingTypeDO;
import cn.iocoder.an.module.business.service.equipmentkeepsettingtype.EquipmentKeepSettingTypeService;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "管理后台 - 保养类型")
@RestController
@RequestMapping("/business/equipment-keep-setting-type")
@Validated
public class EquipmentKeepSettingTypeController {

    @Resource
    private EquipmentKeepSettingTypeService equipmentKeepSettingTypeService;

    @Resource
    private EquipmentKeepTypeService equipmentKeepTypeService;
    @Autowired
    private EquipmentKeepSettingService equipmentKeepSettingService;

    @Autowired
    private EquipmentKeepSettingMapper equipmentKeepSettingMapper;

    @PostMapping("/create")
    @Operation(summary = "创建保养类型")
    @PreAuthorize("@ss.hasPermission('business:equipment-keep-setting-type:create')")
    public CommonResult<Long> createEquipmentKeepSettingType(@Valid @RequestBody EquipmentKeepSettingTypeSaveReqVO createReqVO) {
        return success(equipmentKeepSettingTypeService.createEquipmentKeepSettingType(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新保养类型")
    @PreAuthorize("@ss.hasPermission('business:equipment-keep-setting-type:update')")
    public CommonResult<Boolean> updateEquipmentKeepSettingType(@Valid @RequestBody EquipmentKeepSettingTypeSaveReqVO updateReqVO) {
        equipmentKeepSettingTypeService.updateEquipmentKeepSettingType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除保养类型")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:equipment-keep-setting-type:delete')")
    public CommonResult<Boolean> deleteEquipmentKeepSettingType(@RequestParam("id") Long id) {
        equipmentKeepSettingTypeService.deleteEquipmentKeepSettingType(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除保养类型")
                @PreAuthorize("@ss.hasPermission('business:equipment-keep-setting-type:delete')")
    public CommonResult<Boolean> deleteEquipmentKeepSettingTypeList(@RequestParam("ids") List<Long> ids) {
        equipmentKeepSettingTypeService.deleteEquipmentKeepSettingTypeListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得保养类型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:equipment-keep-setting-type:query')")
    public CommonResult<EquipmentKeepSettingTypeRespVO> getEquipmentKeepSettingType(@RequestParam("id") Long id) {
        EquipmentKeepSettingTypeDO equipmentKeepSettingType = equipmentKeepSettingTypeService.getEquipmentKeepSettingType(id);
        return success(BeanUtils.toBean(equipmentKeepSettingType, EquipmentKeepSettingTypeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得保养类型分页")
    @PreAuthorize("@ss.hasPermission('business:equipment-keep-setting-type:query')")
    public CommonResult<PageResult<EquipmentKeepSettingTypeRespVO>> getEquipmentKeepSettingTypePage(@Valid EquipmentKeepSettingTypePageReqVO pageReqVO) {
        PageResult<EquipmentKeepSettingTypeDO> pageResult = equipmentKeepSettingTypeService.getEquipmentKeepSettingTypePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, EquipmentKeepSettingTypeRespVO.class));
    }


    @GetMapping("/mapList")
    @Operation(summary = "获得已绑定的和未绑定的数据")
    @PreAuthorize("@ss.hasPermission('business:equipment-keep-setting-type:query')")
    public CommonResult<Map<String, List<EquipmentKeepSettingTypeRespVO>>> getEquipmentKeepSettingTypeMapList(@Valid EquipmentKeepSettingTypePageReqVO pageReqVO) {
        return success(equipmentKeepSettingTypeService.getEquipmentKeepSettingTypeMapList(pageReqVO));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出保养类型 Excel")
    @PreAuthorize("@ss.hasPermission('business:equipment-keep-setting-type:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportEquipmentKeepSettingTypeExcel(@Valid EquipmentKeepSettingTypePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<EquipmentKeepSettingTypeDO> list = equipmentKeepSettingTypeService.getEquipmentKeepSettingTypePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "保养类型.xls", "数据", EquipmentKeepSettingTypeRespVO.class,
                        BeanUtils.toBean(list, EquipmentKeepSettingTypeRespVO.class));
    }


    @GetMapping("/get-import-template")
    @Operation(summary = "获得保养设置导入模板导入模板")
    public void importTemplate(HttpServletResponse response) throws IOException {
        // 手动创建导出 demo
        List<EquipmentKeepTypeSettingImportExcelVO> list = Arrays.asList(
                EquipmentKeepTypeSettingImportExcelVO.builder().name("分解炉年保养").cycle(365).remind(365)
                        .status(MaintenanceStatusEnum.run.getStatus()).description("xxxxx描述").build()
        );
        // 输出
        ExcelUtils.write(response, "保养分类导入模板.xlsx", "列表", EquipmentKeepTypeSettingImportExcelVO.class, list);
    }


    @PostMapping("/import")
    @Operation(summary = "导入")
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true),
            @Parameter(name = "updateSupport", description = "是否支持更新，默认为 false", example = "true")
    })
    public CommonResult<ImportRespVO> importExcel(@RequestParam("file") MultipartFile file,
                                                  @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) throws Exception {
        List<EquipmentKeepTypeSettingImportExcelVO> list = ExcelUtils.read(file, EquipmentKeepTypeSettingImportExcelVO.class);
        return success(equipmentKeepSettingTypeService.importList(list, updateSupport));
    }

    @GetMapping("/get-import-templateBind")
    @Operation(summary = "获得保养设置导入模板导入模板")
    public void importTemplateBind(HttpServletResponse response) throws IOException {
        // 手动创建导出 demo
        List<EquipmentSettingBindImportExcelVO> list = Arrays.asList(
                EquipmentSettingBindImportExcelVO.builder().keepTypeName("钎焊炉年保养").name("火焰焊接设备").build()
        );
        // 输出
        ExcelUtils.write(response, "导入模板.xlsx", "列表", EquipmentSettingBindImportExcelVO.class, list);
    }


    @PostMapping("/importBind")
    @Operation(summary = "导入")
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true),
            @Parameter(name = "updateSupport", description = "是否支持更新，默认为 false", example = "true")
    })
    public CommonResult<ImportRespVO> importExcelBind(@RequestParam("file") MultipartFile file,
                                                  @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) throws Exception {
        List<EquipmentSettingBindImportExcelVO> list = ExcelUtils.read(file, EquipmentSettingBindImportExcelVO.class);
        return success(equipmentKeepSettingService.importListBind(list, updateSupport));
    }


}